<html xmlns:th="http://www.thymeleaf.org">

<head>
    <title>教师管理</title>
    <meta charset="UTF-8">
    <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet">
    <!-- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
        integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> -->
    <link rel="stylesheet" href="/css/font-awesome.min.css">
    <script src="/js/jquery.min.js"></script>
    <script src="/js/popper.min.js"></script>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script>
    <style>
        .admin-left {
            height: 100%;
            background-color: #428bca;
            min-width: 120px !important;
        }

        .navbar {
            margin-bottom: 0;
        }

        .row {
            margin: 0 auto;
        }
    </style>
</head>

<body>
<div th:insert="template::header"></div>
<div class="container-fluid">
    <div class="row">
        <div th:insert="template::admin_left"></div>
        <div class="col-lg-10 col-md-10 col-sm-10 col-xs-10">
            <h3>常见问题库</h3>
            <hr/>
            <div style="margin-top: 10px; padding-left:5px;">
                <ol class="breadcrumb">
                    <li><a href="#">常见问题库</a></li>
                </ol>
            </div>
            <div class="col-lg-2 col-sm-2 col-md-2 col-xs-2"></div>
            <div class="col-lg-8 col-sm-8 col-md-8 col-xs-8">
                <div class="admin-right">
                    <button class="btn btn-default" type="submit" id="add_stu" style="margin-top: 5px;">新增问题</button>
                    <form class="form-inline pull-right">
                        <div class="form-group">
                            <label for="exampleInputName2"></label>
                            <input type="text" class="form-control" id="exampleInputName2" placeholder="问题">
                        </div>
                        <button type="submit" class="btn btn-default">查找</button>
                    </form>
                    <table class="table">
                        <tr>
                            <th>题目ID</th>
                            <th>题目标题</th>
                            <th>查看数</th>
                            <th>操作</th>
                        </tr>
                        <tr th:each="question: ${questionList.list}">
                            <td id="id" th:text="${question.id}"></td>
                            <td><a th:text="${question.title}" th:href="@{'/commonQuestion/'+${question.id}}"></a></td>
                            <td th:text="${question.query}"></td>
                            <td>
                                <div th:if="${session.user.teacherId == question.creator}">
                                    <span><a id="questionId" th:value="${question.id}">修改</a></span>
                                    &nbsp
                                    <span><a th:href="@{'/deleteCommonQuestion/'+${question.id}}">删除</a></span>
                                </div>
                            </td>
                        </tr>
                    </table>
                    <nav aria-label="Page navigation">
                        <ul class="pagination">
                            <li th:if="${questionList.isFirstPage == false}">
                                <a href="/FAQ?page=1" aria-label="Previous">
                                    <span aria-hidden="true">&lt;&lt;</span>
                                </a>
                            </li>
                            <li th:if="${questionList.isHasPreviousPage() == true}">
                                <a th:href="@{/FAQ(page=${questionList.pageNum - 1})}" aria-label="Previous">
                                    <span aria-hidden="true">&lt;</span>
                                </a>
                            </li>
                            <li th:each="page : ${#numbers.sequence(1, questionList.pages)}"
                                th:class="${questionList.pageNum == page}?'active':'' ">
                                <a th:href="@{/FAQ(page=${page})}" th:text="${page} "></a>
                            </li>
                            <li th:if="${questionList.isHasNextPage()}">
                                <a th:href="@{/FAQ(page=${questionList.pageNum + 1})}" aria-label="Next">
                                    <span aria-hidden="true">&gt;</span>
                                </a>
                            </li>
                            <li th:if="${questionList.isIsLastPage() == false}">
                                <a th:href="@{/FAQ(page=${questionList.getPages()})}" aria-label="Next">
                                    <span aria-hidden="true">&gt;&gt;</span>
                                </a>
                            </li>
                        </ul>
                    </nav>
                </div>
            </div>
            <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                    aria-hidden="true">&times;</span></button>
                            <h4 class="modal-title" id="myModalLabel">新增</h4>
                        </div>
                        <div class="modal-body">
                            <div class="form-group">
                                <label for="question_title">问题题目</label>
                                <textarea name="question_title" class="form-control" id="question_title"
                                          placeholder="问题题目"></textarea>
                            </div>
                            <div class="form-group">
                                <label for="question_type">问题类型</label>
                                <select id="question_type" name="question_type" onchange="typeChange()">
                                    <option value="1">选择题</option>
                                    <option value="2">简答题</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="question_answer">问题答案</label>
                                <br/>
                                <textarea name="question_answer" class="form-control" id="question_answer"
                                          placeholder="简答答案" style="display: none"></textarea>
                                <div id="question_option_A">
                                    <label for="question_option_A">A</label>
                                    <input type="text" name="question_option_A" class="form-control" id="option_A"
                                           placeholder="A选项">
                                </div>

                                <div id="question_option_B">
                                    <label for="question_option_B">B</label>
                                    <input type="text" name="question_option_B" class="form-control" id="option_B"
                                           placeholder="B选项">
                                </div>
                                <div id="question_option_C">
                                    <label for="question_option_C">C</label>
                                    <input type="text" name="question_option" class="form-control" id="option_C"
                                           placeholder="C选项">
                                </div>
                                <div id="question_option_D">
                                    <label for="question_option_D">D</label>
                                    <input type="text" name="question_option" class="form-control" id="option_D"
                                           placeholder="D选项">
                                </div>
                            </div>
                            <div class="form-group" id="answer_l">
                                <label for="answer">正确选项</label>
                                <select id="answer" name="question_type" onchange="typeChange()">
                                    <option value="A">A</option>
                                    <option value="B">B</option>
                                    <option value="C">C</option>
                                    <option value="D">D</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="question_parse">问题解析</label>
                                <textarea name="question_parse" class="form-control" id="question_parse"
                                          placeholder="问题解析"></textarea>
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default" data-dismiss="modal"><span
                                    class="glyphicon glyphicon-remove" aria-hidden="true"></span>关闭
                            </button>
                            <button type="button" id="btn_submit" class="btn btn-primary" data-dismiss="modal"><span
                                    class="glyphicon glyphicon-floppy-disk" aria-hidden="true"></span>保存
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-lg-2 col-sm-2 col-md-2 col-xs-2"></div>
    </div>


</div>

<script>
    $(function () {
        $(".panel-heading").click(function (e) {
            /*切换折叠指示图标*/
            $(this).find("span").toggleClass("glyphicon-chevron-down");
            $(this).find("span").toggleClass("glyphicon-chevron-up");
        });
    });
    //注册新增按钮的事件
    $("#add_stu").click(function () {
        $("#myModalLabel").text("新增");
        $('#myModal').modal();
    });

    $('#questionId').click(function updateQuestion(){
        $.ajax({
            url: '/queryCommonQuestionById_JSON/'+$('#id').val,
            method: 'get',
            success:
                function (data) {
                    $('#myModal').modal();
                    options = data.options
                    $("#question_title").val(data.title);
                    $("#question_type").val(data.type);
                    typeChange()
                    $("#option_A").val(data.options[0])
                    $("#option_B").val(data.options[1])
                    $("#option_C").val(data.options[2])
                    $("#option_D").val(data.options[3])
                    if (type == "1") {
                        answer = $("#answer").val(data.answer);
                    } else {
                        answer = $("#question_answer").val(data.answer)
                    }
                    $("#question_parse").val(data.parse)
                }
        })
    })
    function typeChange() {
        var type = $("#question_type").val();
        if (type === "1") {
            $("#question_answer").css("display", "none");
            $("#question_option_A").css("display", "block");
            $("#question_option_B").css("display", "block");
            $("#question_option_C").css("display", "block");
            $("#question_option_D").css("display", "block");
            $("#answer").css("display", "block");
            $("#answer_l").css("display", "block");
        } else {
            $("#question_answer").css("display", "block");
            $("#question_option_A").css("display", "none");
            $("#question_option_B").css("display", "none");
            $("#question_option_C").css("display", "none");
            $("#question_option_D").css("display", "none");
            $("#answer").css("display", "none");
            $("#answer_l").css("display", "none");
        }
    }

    $("#btn_submit").click(
        function () {
            const title = $("#question_title").val();
            const type = $("#question_type").val();
            let answer;
            let option = [$("#option_A").val(), $("#option_B").val(), $("#option_C").val(), $("#option_D").val()]
            if (type == "1") {
                answer = $("#answer").val();
            } else {
                answer = $("#question_answer").val()
            }
            $.ajax(
                {
                    url: "/createCommonQuestion",
                    method: "post",
                    contentType: "application/json;charset=UTF-8",
                    dataType: "json",
                    data: JSON.stringify({
                        "title": title,
                        "type": type,
                        "options": option,
                        "answer": answer,
                        "parse": $("#question_parse").val()
                    }),
                    success: function (data) {
                        if (data.code == 200) {
                            alert("添加问题成功!");
                        } else {
                            alert("添加问题失败!");
                        }
                    },
                    fail: function (data) {
                        alert("系统异常!");
                    }
                }
            )
        }
    )
</script>
</body>

</html>